﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using IData;

namespace Data
{
    public class DbLogin : IDbLogin
    {
        string str;
        public DbLogin()
        {
            str = ConfigurationManager.AppSettings["ConnectionString"];
        }
        /// <summary>
        /// 增加\删除\修改
        /// </summary>
        /// <param name="LineQ">语句</param>
        public int add(string sql)
       {
           int result=0;
         SqlConnection conn = new SqlConnection(str);
         conn.Open();
         SqlCommand cmd = new SqlCommand(sql,conn);
         try
         {
              result = cmd.ExecuteNonQuery();
         }
         catch (SqlException ex)
         {
             throw ex;
         }
         finally { 
         conn.Close();
         cmd.Dispose();
         }
         return result;

       }
        /// <summary>
        /// 查询表
        /// </summary>
        /// <param name="sql">语句</param>
        /// <returns></returns>
        public DataTable select(string  sql)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                sda.Fill(dt); 
            }
           catch (SqlException ex)
         {
             throw ex;
         }
            finally { 
            conn.Close();
            cmd.Dispose();
            }
            
            return dt;
        }
        /// <summary>
        /// 获取存储过程返回的bool值
        /// </summary>
        /// <param name="procedureName">存储过程名称</param>
        /// <param name="parameters">参数</param>
        /// <returns>bool</returns>
        public bool selectBool(string procedureName, params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            SqlCommand cmd = new SqlCommand(procedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (var item in parameters)
            {
                cmd.Parameters.Add(item);
            }
            cmd.Parameters.AddWithValue("@return", "").Direction = ParameterDirection.ReturnValue;
            cmd.ExecuteNonQuery();
            bool dt = false;　　　　　　
            try
            {
                var result=(int)cmd.Parameters["@return"].Value;
                if (result== 1)
                {
                    dt = true;
                }
                if (result != 1)
                {
                    dt = false;
                }

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                cmd.Dispose();
            }

            return dt;
        }
    }
}
